<!DOCTYPE html>
<html>
<head>
<title>搜索商品</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="viewport" id="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no"/>
<link href="/static/webAPP/css/reset.css" rel="stylesheet" type="text/css">
<link href="/static/webAPP/css/discounts.css" rel="stylesheet" type="text/css">
<script src="/static/webAPP/js/jquery-1.8.3.min.js"></script>
<script src="/static/webAPP/js/base.js"></script>
<style>
	.search-hot{
		color: lightblue;
	}
	.discounts-bigbox{
		margin-top: 55px;
	}
</style>
</head>
<body>
<div class="near-box">
      <!--头部开始-->
	  <div class="header">
	  	 <a href="javascript:history.go(-1);" class="left"></a>
	  	 搜索词:<span class="search-hot"></span>
	  </div>
	  <!--头部结束-->
      <div class="discounts-bigbox discounts_info">
		  <div class="discounts-box1">
		  	 检查网络是否正常或是否登录
		  	 </div>
		  </div>

      </div>
      <!--bigbox结束-->
      <div class="discounts-bottom">
      </div>
      
</div>
<script src="/static/webAPP/lib/mobile/layer.js"></script>
<script type="text/javascript" src="/static/webAPP/js/scrolltopcontrol.js"></script>
<script>
	$(function(){
		var areaId = sessionStorage.getItem("citycode")
		var userName = sessionStorage.getItem("sessionName")
		var user_id = sessionStorage.getItem("sessionUserid")
		var tag = ''
		var commodityName = getUrlParam("id")
		$(".search-hot").html(decodeURI(commodityName))
		$.ajax({
			type: "get",
							//TODO:
			url: "/app/commodityInfo/getCommodityInfoByName2/"+ areaId +"/"+ commodityName +"/1",
			data: '',
			dataType: "json",
			success: function(mag){
				var data = mag.data
				for (let i = 0; i < data.length; i++) {
					var tag1 = '<div class="discounts-box1"><a href="/static/webAPP/goods-details.html?id='+ data[i].id +'" class="d-box1-img1"><img src="https://vm.fowotek.com/'+ getSmallUrl(data[i].picture_url) +'" alt=""></a><div class="discounts-box2" cid="'+ data[i].id +'" specificationsid="'+ data[i].specification_id +'"><a href="/static/webAPP/goods-details.html?id='+ data[i].id +'" class="d-box2-text1">'+ data[i].commodityName +'</a><span class="d-box2-text2">'+ data[i].specifications +'</span><span class="d-box2-text3">￥'+ data[i].price +'</span><a href="javascript:void(0);" class="d-box2-btn"><img src="/static/webAPP/images/gouwuchee.png"></a></div></div>'
					tag += tag1
				}
				$(".discounts_info").html(tag)
			}
		})


		$(document).on('click','.d-box2-btn',function(){
			var id = $(this).closest(".discounts-box2").attr("cid")
			var specificationsid = $(this).closest(".discounts-box2").attr("specificationsid")
			if (userName == null || userName == '' || userName == undefined) {
				layer.open({
					content: '请先去登录账号',
					skin: 'msg',
					time: 2 //2秒后自动关闭
				});
			}else{
				
				$.ajax({
					type: "post",
							//TODO:
					url: "/app/basket/addCommodityToBasket",
					data: {commodity_id: id,commodity_specification_id: specificationsid,count: 1,user_id: user_id},
					dataType: "json",
					success: function(res){
						layer.open({
							content: res.message,
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
					},
					error: function(){
						layer.open({
							content: '商品信息错误，请联系客服',
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
					}
				})
			}
		})

		var num = 1
		$(window).scroll(function() {
			var scrollTop = $(this).scrollTop(),scrollHeight = $(document).height(),windowHeight = $(this).height();
			var positionValue = (scrollTop + windowHeight) - scrollHeight;
			if (positionValue == 0) {
				num ++
				$.ajax({
					type: "get",
							//TODO:
					url: "/app/commodityInfo/getCommodityInfoByName2/"+ areaId +"/"+ commodityName +"/"+ num,
					data: '',
					dataType: "json",
					success: function(mag){
						var data = mag.data
						for (let i = 0; i < data.length; i++) {
							var tag1 = '<div class="discounts-box1"><a href="/static/webAPP/goods-details.html?id='+ data[i].id +'" class="d-box1-img1"><img src="https://vm.fowotek.com/'+ getSmallUrl(data[i].picture_url) +'" alt=""></a><div class="discounts-box2" cid="'+ data[i].id +'" specificationsid="'+ data[i].specification_id +'"><a href="/static/webAPP/goods-details.html?id='+ data[i].id +'" class="d-box2-text1">'+ data[i].commodityName +'</a><span class="d-box2-text2">'+ data[i].specifications +'</span><span class="d-box2-text3">￥'+ data[i].price +'</span><a href="javascript:void(0);" class="d-box2-btn"><img src="/static/webAPP/images/gouwuchee.png"></a></div></div>'
							tag += tag1
						}
						if (data.length != 10) {
							$(".discounts_info").html(tag + '<div style="text-align: center;color: #ccc;width:100%;float:left">已加载全部商品</div>')
						}else{
							$(".discounts_info").html(tag)
						}
					}
				})
			}
		});
	})



function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
	var context = "";
	if (r != null){
		context = r[2];
		reg = null;
		r = null;
	}
	return context == null || context == "" || context == "undefined" ? "" : context;
}
</script>
</body>
</html>